package com.myebills.ebpsfrontend.cust.maxis;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.myebills.ebpsfrontend.cust.utils.*;
import com.checkfree.isolutions.customer.persist.*;
import com.checkfree.isolutions.admin.ObjectId;
import com.checkfree.isolutions.statement.persist.Statement;
import com.checkfree.isolutions.utility.CFiError;
import java.util.Vector;
import java.util.*;
import java.text.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ibm.logging.ILogger;
import com.ibm.logging.IRecordType;

/**
 * Module Name  : CusRecentBillsServlet_v1r00
 * Description  : Bill Listing for customer
 * Last Updated : 2002-10-10
 *
 * @author lhlue
 *
 * Remarks :
 *
 */

public class CusRecentBillsServlet_v1r00 extends EbppxTransactionServlet {
    //retrieve previous 6 months statements
    ILogger trcLogger = null;

    public void doTransaction(
    HttpServletRequest req,
    HttpServletResponse res)
    throws ServletException {

        final String fName = "doTransaction";
        trcLogger = getTraceLogger(req);

        if (trcLogger.isLogging())
            trcLogger.entry(IRecordType.TYPE_INFO, this, fName);

        try {
            final HttpSession session = req.getSession(false);

            if (null == session)
                throw new IllegalStateException("no session established");

		    final String randomKey = (String) session.getAttribute(EbppxConstants.RANDOM_KEY);

            //Get attribute from session for the logon user
            final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);

            EbppxUrlManager urlManager = getUrlManager(req);

            // get the account no selected in JSP
            String strAccNo = urlManager.getRequestParameter("accNo");
            //String decryptedAccNo = EncryptionHelper.decrypt(strAccNo,randomKey);
            System.out.println("strAccNo at recent bills servlet:"+strAccNo);

            //get the vector of Accounts
            final Vector cfiAccounts = ecsUser.getAccounts();
            final Vector cfiAcctExtKeys = ecsUser.getAccountExternalKeys();

//poon test start
            final Vector cfiOldAccounts = ecsUser.getOldAccounts();
            req.setAttribute("oldAccounts",cfiOldAccounts);

			//to get old acc no for selected new acc no
			String oldAccountNo = ecsUser.getOldAccount(cfiAcctExtKeys, cfiOldAccounts, strAccNo);
//poon test end

			//to check is there any old account number - start
			int oldAccountDisplayCount = 0;
			String oldAccountDisplay = "N";
			for (java.util.Enumeration enumAccounts = cfiAcctExtKeys.elements(); enumAccounts.hasMoreElements();) {
				final String tAccountNo = (String) enumAccounts.nextElement();

				if (!AcctDetails.getAcctKey(tAccountNo).equals("")) {
					oldAccountDisplayCount++;
				}

			}
			if (oldAccountDisplayCount > 0) {
				oldAccountDisplay = "Y";
			}
            req.setAttribute("oldAccountDisplay",oldAccountDisplay);
			//to check is there any old account number - end

            req.setAttribute(EbppxConstants.CFI_ACCOUNTS, cfiAccounts);
            req.setAttribute("acctExtKeys",cfiAcctExtKeys);

            //if an account has been selected then get the statements
            if (strAccNo !=null && !strAccNo.equals("")) {
                if (trcLogger.isLogging())
                    trcLogger.text(IRecordType.TYPE_INFO, this, fName,"requested acc no: "+strAccNo);

                Merchant cfiMerchant = MerchantHomeImpl.singleton().findByName(ecsUser.getMerchantName());
                Account account = AccountHomeImpl.singleton().findByExternalKeyAndMerchant(strAccNo, cfiMerchant);
                Vector cfiStatements =  StatementUtil.getStatements(account,EbppxCusConstants.STMT_START_MONTH);

                req.setAttribute(EbppxConstants.CFI_STATEMENTS, cfiStatements);
            }

            req.setAttribute("accNo", strAccNo);
            session.setAttribute("fromPage","0");

            forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);
        }
        catch (Throwable t) {
			System.out.println(t.toString());
            throw new ServletException(t.toString(), t);
        }
        finally {
            if (trcLogger.isLogging())
                trcLogger.exit(IRecordType.TYPE_INFO, this, fName);
        }

    }

}




